# -*- coding: utf-8 -*-
# coding=utf-8
import os
import shutil
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

class Utils:

    @staticmethod
    def copyFiles(sourceDir,  targetDir):
        print sourceDir, targetDir
        if sourceDir.find(".svn") > 0:
            return
        for file in os.listdir(sourceDir):
            sourceFile = os.path.join(sourceDir,  file)
            targetFile = os.path.join(targetDir,  file)
            if os.path.isfile(sourceFile):
                if not os.path.exists(targetDir):
                    os.makedirs(targetDir)
                if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
                        open(targetFile, "wb").write(open(sourceFile, "rb").read())
            if os.path.isdir(sourceFile):
                Utils.copyFiles(sourceFile, targetFile)


class Theme:

    _current_path = None

    def __init__(self):
        self._current_path = os.getcwd().replace("\\", "/")
        pass

    def prepareCopy(self, resource):
        tmp_works = self._current_path + "/PrivacyThemeApk"
        shutil.rmtree(tmp_works, True)
        Utils.copyFiles(self._current_path + "/bin/PrivacyThemeApk", tmp_works)
        res_ = self._current_path + "/source/" + resource + "/res"
        Utils.copyFiles(res_, tmp_works + "/app/src/main/res")

    def preparePackageName(self, packageName):
        tmp_works = self._current_path + "/PrivacyThemeApk/app/build.gradle"

        f = open(tmp_works, "r")
        content = f.readlines()
        l = len(content)
        for i in range(l):
            content[i] = content[i].replace("com.zxl.manager.privacy.zip.theme", "com.zxl.manager.privacy.zip.theme." + packageName)
        f.close()

        f = open(tmp_works, "w")
        f.writelines(content)
        f.flush()
        f.close()

    def prepareAppName(self, appName):
        tmp_works = self._current_path + "/PrivacyThemeApk/app/src/main/res/values/strings.xml"

        f = open(tmp_works, "r")
        content = f.readlines()
        l = len(content)
        for i in range(l):
            content[i] = content[i].replace("PrivacyThemeApk", appName)
        f.close()

        f = open(tmp_works, "wb")
        f.writelines(content)
        f.flush()
        f.close()

    def removeFile(self, dir, postfix):
      if os.path.isdir(dir):
          for file in os.listdir(dir):
              self.removeFile(dir+'/'+file, postfix)
      else:
          if os.path.basename(dir) == postfix:
              print dir
              os.remove(dir)


    def build(self, name):
        os.chdir(self._current_path + "/PrivacyThemeApk")
        os.system("gradle clean assembleRelease")

        targetDir = self._current_path + "/dst/apk/" + name

        if not os.path.exists(targetDir):
            os.makedirs(targetDir)

        apk_dir = self._current_path + "/PrivacyThemeApk/app/build/outputs/apk"

        apk_name = "/app-baidu-release.apk"
        open(targetDir + apk_name, "wb").write(open(apk_dir + apk_name, "rb").read())

        apk_name = "/app-qq-release.apk"
        open(targetDir + apk_name, "wb").write(open(apk_dir + apk_name, "rb").read())

        apk_name = "/app-other-release.apk"
        open(targetDir + apk_name, "wb").write(open(apk_dir + apk_name, "rb").read())

    def main(self):
        file_list = os.listdir(self._current_path + "/source")
        for num in range(len(file_list)):
            filename = file_list[num]
            tmp = filename.split("_")

            app_name = tmp[0].decode("gbk", "ignore").encode("utf-8")
            package_name = tmp[1]

            print app_name, package_name
            self.prepareCopy(filename)

            self.preparePackageName(package_name)
            self.prepareAppName(app_name)

            self.removeFile(self._current_path + "/PrivacyThemeApk", "Thumbs.db")
            self.removeFile(self._current_path + "/PrivacyThemeApk", "change style.png")

            self.build(package_name)

        shutil.rmtree(self._current_path + "/PrivacyThemeApk", True)

t = Theme()
t.main()


